package com.itouchtv.yang.library.page;

import com.itouchtv.yang.core.tools.AnalysisXml;
import com.itouchtv.yang.core.tools.Log;
import org.dom4j.Element;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 根据Library.xml，自动生成Page类
 * 运行前需要配置pageName和fileName
 * pageName：Library内page标签的name，通过这个变量配置为哪个页面生成page类
 * fileName:配置类名，即java文件的名称。
 */
@Deprecated
public class AutoCreatePage {

    String pageName = "releaseArticle";
    String fileName = "ReleaseArticlePage";
    File file;

    public static void main(String agrs[]){
        Log.init();
        AutoCreatePage autoCreatePage = new AutoCreatePage();
        autoCreatePage.createFile();
        autoCreatePage.wrinteFile();
    }

    private void createFile(){
        String filePath = "./src/test/java/com/itouchtv/yang/library/page/";
        file = new File(filePath);
        if (file.exists()){
            file = new File(filePath+fileName+".java");
            if (!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("创建文件失败");
                }
            }else System.out.println("已经存在同名文件："+file.getPath());
        }else System.out.println("没有找到目标目录:"+file.getPath());
    }

    private void wrinteFile(){
        List<Element> elements = new ArrayList<Element>();
        AnalysisXml analysisXml = new AnalysisXml();
        analysisXml.getPageElements(pageName);
        elements = analysisXml.getPageElements(pageName);
        FileWriter output;
        StringBuffer sb;


        sb = new StringBuffer("package com.itouchtv.yang.library.page;\n");
        sb.append("import com.itouchtv.yang.core.tools.AnalysisXml;\n");
        sb.append("import org.openqa.selenium.WebDriver;\n");
        sb.append("import org.openqa.selenium.WebElement;\n");
        sb.append("import org.testng.Assert;\n");
        sb.append("\n");
        sb.append("public class "+fileName+" extends PageObject{\n");
        sb.append("\n");
        sb.append("    private AnalysisXml analysisXml = null;\n");

        for (Element e:elements){
            sb.append("    private WebElement "+e.getText()+"= null;\n");
        }

        sb.append("\n");

        sb.append("    public "+fileName+"(WebDriver webDriver){\n" +
                "        super(webDriver);\n" +
                "        analysisXml = new AnalysisXml();\n" +
                "        initElement();\n" +
                "    }\n");

        sb.append("\n");

        sb.append("    @Override\n" +
                "    protected void initElement() {\n" +
                "        analysisXml.getPageElements(\""+ pageName +"\");\n" +
                "    }\n");

        for (Element e:elements){
            String s = e.getText();
            sb.append("\n");
            sb.append("    public void "+s+"Action(){\n" +
                    "        if("+s+"==null){\n" +
                    "            "+s+" = analysisXml.findElement(webDriver,\""+s+"\");\n" +
                    "        }\n" +
                    "    }\n");
        }

        sb.append("}\n");

        try {
            output = new FileWriter(file);
            output.write(sb.toString());
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
